<?php
if(!defined('inside')) die('You do not have permission to execute this file directly.');

/**
* Email class file.
*
* @author Federico Ramirez <fedekiller@gmail.com>
* @link http://code.google.com/p/akaikiwi/
* @copyright Copyright &copy; 2008-2011 Federico Ramirez
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
*/

/*
* This class will easier the process of sending emails.
*/
class Email {
	private $to;
	private $from;
	private $cc;
	private $bcc;
	private $message;
	private $subject;
	private $html;
	private $attachment;

	function __construct() {
		$cfg = AkaiKiwi::loadLib('Config');
		$this->from = $cfg->get_item('emailsFrom');
		$this->html = FALSE;
		$this->attachment = array();
	}

	public function set_to($to) {
		$this->to = $to;
	}

	public function set_from($from) {
		$this->from = $from;
	}

	public function set_cc($cc) {
		$this->cc = cc;
	}

	public function set_bcc($bcc) {
		$this->bcc = $bcc;
	}

	public function set_message($msg) {
		$this->message = $msg;
	}

	public function set_subject($subject) {
		$this->subject = $subject;
	}

	public function set_html($html) {
		$this->html = $html;
	}

	public function add_attachment($attachment) {
		$this->attachment[] = $attachment;
	}

	public function send() {
		if(empty($this->subject) || empty($this->message) || empty($this->to)) {
			throw new AkaikiwiException('You must at least introduce a subject and a message to send a mail.');
		}

		// Headers
		$uid = md5(uniqid(time()));
		$header = 'To: ' . $this->to . "\r\n" .
		'From: ' . $this->from . "\r\n";

		if(!empty($this->cc)) {
			$header .= 'Cc: ' . $this->cc . "\r\n";
		}
		if(!empty($this->bcc)) {
			$header .= 'Bcc: ' . $this->bcc . "\r\n";
		}

		$header .= "Reply-To: " . $this->from . "\r\n";
		$header .= "MIME-Version: 1.0\r\n";
		$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
		$header .= "This is a multi-part message in MIME format.\r\n";
		$header .= "--".$uid."\r\n";
		if($this->html) {
			$header .= "Content-type:text/html; charset=UTF-8\r\n";
		} else {
			$header .= "Content-type:text/plain; charset=UTF-8\r\n";
		}
		$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
		$header .= $this->message."\r\n\r\n";
		$header .= "--".$uid."\r\n";

		if(count($this->attachment) > 0) {
			foreach($this->attachment as $attachment) {
				if(!file_exists($attachment)) {
					throw new AkaikiwiException('Could not find file ' . $attachment);
				}

				$handle = fopen($attachment, 'r');
				$content = fread($handle, filesize($attachment));
				fclose($handle);
				$content = chunk_split(base64_encode($content));

				$header .= 'Content-Type: ' . $this->get_mime_type(substr($attachment, -3)) . "; name=\"".$attachment."\"\r\n";
				$header .= "Content-Transfer-Encoding: base64\r\n";
				$header .= "Content-Disposition: attachment; filename=\"" . $attachment . "\"\r\n\r\n";
				$header .= $content."\r\n\r\n";
				$header .= "--".$uid."--";
			}
		}
		// End of headers

		// Send
		return mail($this->to, $this->subject, /*$this->message*/ '', $header);
	}

	private function get_mime_type($ext = "") {
		$mimes = array(
			'hqx'	=>	'application/mac-binhex40',
			'cpt'	=>	'application/mac-compactpro',
			'doc'	=>	'application/msword',
			'bin'	=>	'application/macbinary',
			'dms'	=>	'application/octet-stream',
			'lha'	=>	'application/octet-stream',
			'lzh'	=>	'application/octet-stream',
			'exe'	=>	'application/octet-stream',
			'class'	=>	'application/octet-stream',
			'psd'	=>	'application/octet-stream',
			'so'	=>	'application/octet-stream',
			'sea'	=>	'application/octet-stream',
			'dll'	=>	'application/octet-stream',
			'oda'	=>	'application/oda',
			'pdf'	=>	'application/pdf',
			'ai'	=>	'application/postscript',
			'eps'	=>	'application/postscript',
			'ps'	=>	'application/postscript',
			'smi'	=>	'application/smil',
			'smil'	=>	'application/smil',
			'mif'	=>	'application/vnd.mif',
			'xls'	=>	'application/vnd.ms-excel',
			'ppt'	=>	'application/vnd.ms-powerpoint',
			'wbxml'	=>	'application/vnd.wap.wbxml',
			'wmlc'	=>	'application/vnd.wap.wmlc',
			'dcr'	=>	'application/x-director',
			'dir'	=>	'application/x-director',
			'dxr'	=>	'application/x-director',
			'dvi'	=>	'application/x-dvi',
			'gtar'	=>	'application/x-gtar',
			'php'	=>	'application/x-httpd-php',
			'php4'	=>	'application/x-httpd-php',
			'php3'	=>	'application/x-httpd-php',
			'phtml'	=>	'application/x-httpd-php',
			'phps'	=>	'application/x-httpd-php-source',
			'js'	=>	'application/x-javascript',
			'swf'	=>	'application/x-shockwave-flash',
			'sit'	=>	'application/x-stuffit',
			'tar'	=>	'application/x-tar',
			'tgz'	=>	'application/x-tar',
			'xhtml'	=>	'application/xhtml+xml',
			'xht'	=>	'application/xhtml+xml',
			'zip'	=>	'application/zip',
			'mid'	=>	'audio/midi',
			'midi'	=>	'audio/midi',
			'mpga'	=>	'audio/mpeg',
			'mp2'	=>	'audio/mpeg',
			'mp3'	=>	'audio/mpeg',
			'aif'	=>	'audio/x-aiff',
			'aiff'	=>	'audio/x-aiff',
			'aifc'	=>	'audio/x-aiff',
			'ram'	=>	'audio/x-pn-realaudio',
			'rm'	=>	'audio/x-pn-realaudio',
			'rpm'	=>	'audio/x-pn-realaudio-plugin',
			'ra'	=>	'audio/x-realaudio',
			'rv'	=>	'video/vnd.rn-realvideo',
			'wav'	=>	'audio/x-wav',
			'bmp'	=>	'image/bmp',
			'gif'	=>	'image/gif',
			'jpeg'	=>	'image/jpeg',
			'jpg'	=>	'image/jpeg',
			'jpe'	=>	'image/jpeg',
			'png'	=>	'image/png',
			'tiff'	=>	'image/tiff',
			'tif'	=>	'image/tiff',
			'css'	=>	'text/css',
			'html'	=>	'text/html',
			'htm'	=>	'text/html',
			'shtml'	=>	'text/html',
			'txt'	=>	'text/plain',
			'text'	=>	'text/plain',
			'log'	=>	'text/plain',
			'rtx'	=>	'text/richtext',
			'rtf'	=>	'text/rtf',
			'xml'	=>	'text/xml',
			'xsl'	=>	'text/xml',
			'mpeg'	=>	'video/mpeg',
			'mpg'	=>	'video/mpeg',
			'mpe'	=>	'video/mpeg',
			'qt'	=>	'video/quicktime',
			'mov'	=>	'video/quicktime',
			'avi'	=>	'video/x-msvideo',
			'movie'	=>	'video/x-sgi-movie',
			'doc'	=>	'application/msword',
			'word'	=>	'application/msword',
			'xl'	=>	'application/excel',
			'eml'	=>	'message/rfc822'
			);

			return (!isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
	}
}
